using System;

namespace CoverageRunner
{
    public class CoverageGenerator
    {
        private readonly string _vstsCoverageOutputFile;
        private readonly UnitTestRunner _unitTestRunner;
        private readonly CoverageReportCreator _reportCreator;

        public CoverageGenerator(string vstsCoverageOutputFile, UnitTestRunner unitTestRunner, CoverageReportCreator reportCreator)
        {
            _vstsCoverageOutputFile = vstsCoverageOutputFile;
            _unitTestRunner = unitTestRunner;
            _reportCreator = reportCreator;
        }

        public void Execute()
        {
            using (new ProfilerRunner())
            using (new Runner(_vstsCoverageOutputFile))
            {
                var output = _unitTestRunner.Execute();
                Console.WriteLine(output);
                //TODO: Modify this, shuould not throw an exception but log this into a file to report later on
                if (_unitTestRunner.ExitCode != 0)
                    throw new ApplicationException("Unable to successfully execute the test runner");
            }
            _reportCreator.Create();
        }
    }
}